package com.cmc.cloud.cmclink.doc.api.document;

import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.api.document.rep.rule.LimitRuleRespDto;
import com.cmc.cloud.cmclink.doc.convert.LimitRuleAttatchConvert;
import com.cmc.cloud.cmclink.doc.convert.LimitRuleConfirmItemConvert;
import com.cmc.cloud.cmclink.doc.convert.LimitRuleConvert;
import com.cmc.cloud.cmclink.doc.convert.LimitRuleItemConvert;
import com.cmc.cloud.cmclink.doc.entity.LimitAttatchItemDO;
import com.cmc.cloud.cmclink.doc.entity.LimitConfirmItemDO;
import com.cmc.cloud.cmclink.doc.entity.LimitRuleDO;
import com.cmc.cloud.cmclink.doc.entity.LimitRuleItemDO;
import com.cmc.cloud.cmclink.doc.mapper.LimitAttatchItemMapper;
import com.cmc.cloud.cmclink.doc.mapper.LimitConfirmItemMapper;
import com.cmc.cloud.cmclink.doc.mapper.LimitRuleItemMapper;
import com.cmc.cloud.cmclink.doc.mapper.LimitRuleMapper;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author  lzp
 */
@RestController
public class LimitRuleApiImpl implements LimitRuleApi {


    @Resource
    private LimitRuleMapper limitRuleMapper;

    @Resource
    private LimitRuleItemMapper limitRuleItemMapper;

    @Resource
    private LimitAttatchItemMapper limitAttatchItemMapper;

    @Resource
    private LimitConfirmItemMapper limitConfirmItemMapper;

    @Override
    public CommonResult<List<LimitRuleRespDto>> queryRuleInfo(String ruleType) {
        LambdaQueryWrapperX<LimitRuleDO> queryWrapperX = new LambdaQueryWrapperX<LimitRuleDO>()
                .eq(LimitRuleDO::getRuleType, ruleType)
                .eq(LimitRuleDO::getEnable, true);
        List<LimitRuleDO> limitRuleDoList = limitRuleMapper.selectList(queryWrapperX);
        if (CollectionUtil.isEmpty(limitRuleDoList)) {
            return CommonResult.success(Collections.emptyList());
        }
        List<LimitRuleRespDto> limitRuleRespDtoList = new ArrayList<>(16);
        List<Long> mainIds = limitRuleDoList.stream().map(LimitRuleDO::getId).collect(Collectors.toList());
        LambdaQueryWrapperX<LimitRuleItemDO> ruleItemQueryWrapper = new LambdaQueryWrapperX<LimitRuleItemDO>()
                .in(LimitRuleItemDO::getMainId, mainIds);
        //处理子禁接规则
        Map<Long, List<LimitRuleItemDO>> ruleItemListMap = limitRuleItemMapper.selectList(ruleItemQueryWrapper).stream().collect(Collectors.groupingBy(LimitRuleItemDO::getMainId));
        //附件信息
        LambdaQueryWrapperX<LimitAttatchItemDO> ruleAttatchQueryWrapper = new LambdaQueryWrapperX<LimitAttatchItemDO>()
                .in(LimitAttatchItemDO::getMainId, mainIds);
        Map<Long, List<LimitAttatchItemDO>> ruleAttatchItemListMap = limitAttatchItemMapper.selectList(ruleAttatchQueryWrapper).stream().collect(Collectors.groupingBy(LimitAttatchItemDO::getMainId));
        //确认信息
        LambdaQueryWrapperX<LimitConfirmItemDO> ruleConfirmQueryWrapper = new LambdaQueryWrapperX<LimitConfirmItemDO>()
                .in(LimitConfirmItemDO::getMainId, mainIds);
        Map<Long, List<LimitConfirmItemDO>> ruleConfirmItemListMap = limitConfirmItemMapper.selectList(ruleConfirmQueryWrapper).stream().collect(Collectors.groupingBy(LimitConfirmItemDO::getMainId));

        limitRuleDoList.stream().forEach(limitRuleDo -> {
            LimitRuleRespDto limitRuleRespDto = new LimitRuleRespDto();
            limitRuleRespDto = LimitRuleConvert.INSTANCE.convertLimitRuleRespDto(limitRuleDo);
            Long mainId = limitRuleDo.getId();
            //处理子禁接规则
            if (ruleItemListMap.containsKey(mainId)) {
                limitRuleRespDto.setRuleItem(LimitRuleItemConvert.INSTANCE.convertLimitRuleRespDto(ruleItemListMap.get(mainId).get(0)));
            }
            //附件信息
            if (ruleAttatchItemListMap.containsKey(mainId)) {
                limitRuleRespDto.setAttatchItemList(LimitRuleAttatchConvert.INSTANCE.convertLimitRuleRespDto(ruleAttatchItemListMap.get(mainId)));
            }
            //确认信息
            if (ruleConfirmItemListMap.containsKey(mainId)) {
                limitRuleRespDto.setConfirmItemList(LimitRuleConfirmItemConvert.INSTANCE.convertLimitRuleRespDto(ruleConfirmItemListMap.get(mainId)));
            }
            limitRuleRespDtoList.add(limitRuleRespDto);
        });
        return CommonResult.success(limitRuleRespDtoList);
    }
}
